var categoriesRequest;
var school;

function loadCategories(level)
{
	if (categoriesRequest != null)
		if (categoriesRequest.readyState != 4)
			return;
	categoriesRequest = false;
	if (window.XMLHttpRequest) // Mozilla, Safari,...
	{
		categoriesRequest = new XMLHttpRequest();
		if (categoriesRequest.overrideMimeType)
			categoriesRequest.overrideMimeType('text/xml');
	}
	else if (window.ActiveXObject) // IE
	{
		try
		{
			categoriesRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				categoriesRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!categoriesRequest)
		return false;
	school = level;
	categoriesRequest.onreadystatechange = function() {
		if ((categoriesRequest.readyState == 4) && (categoriesRequest.status == 200))
		{
			for (i = 0; i < categoriesRequest.responseXML.childNodes[0].childNodes.length; i++)
			{
				categoriesSelect = document["tsearch" + (i + 1)]["select_category_" + i];
				if (!categoriesSelect)
					continue;
				if (i != school)
				{
					var option = document.createElement('option');
					option.innerHTML = '--- wybierz przedmiot ---';
					option.value = '';
					categoriesSelect.appendChild(option);
				}
				for (j = 0; j < categoriesRequest.responseXML.childNodes[0].childNodes[i].childNodes.length; j++)
				{
					var optgroup = document.createElement('optgroup');
					optgroup.label = categoriesRequest.responseXML.childNodes[0].childNodes[i].childNodes[j].attributes[0].nodeValue;
					for (k = 0; k < categoriesRequest.responseXML.childNodes[0].childNodes[i].childNodes[j].childNodes.length; k++)
					{
						var option = document.createElement('option');
						option.innerHTML = categoriesRequest.responseXML.childNodes[0].childNodes[i].childNodes[j].childNodes[k].childNodes[0].nodeValue;
						option.value = categoriesRequest.responseXML.childNodes[0].childNodes[i].childNodes[j].attributes[1].nodeValue + "/" +
							categoriesRequest.responseXML.childNodes[0].childNodes[i].childNodes[j].childNodes[k].attributes[0].nodeValue + "/";
						optgroup.appendChild(option);
					}
					categoriesSelect.appendChild(optgroup);
				}
			}
			if (typeof(selectCurrentCategory) == 'function')
				selectCurrentCategory();
		}
	};
    categoriesRequest.open('GET', '/teksty/categories.php', true);
	categoriesRequest.send(null);
}

function writePerPage(query, perPage, active)
{
	document.write('<a href="?query='+query+'&pp='+perPage+'&page=0">');
	if (active)
		document.write(perPage);
	else
		document.write('<u>'+perPage+'</u>');
	document.write('</a> ');
}

function writeSort(sortOrder, sortDirection, active)
{
	document.write('<a href="?sort='+sortOrder+'&dir='+(sortDirection?1:0)+'&page=0">');
	document.write('<img src="http://gfx1.bryk.pl/graphics/sort_'+(sortDirection?'up':'down')+(active?'':'_checked')+'.gif" alt="" />');
	document.write('</a> ');
}
